//
//  HomeTableViewCell.m
//  B2C
//
//  Created by xiaowu on 2017/7/25.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "HomeTableViewCell.h"

@interface HomeTableViewCell ()<UITableViewDataSource, UITableViewDelegate,BHInfiniteScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *normalArr;

@property (nonatomic, strong) UIView *tableHeaderView;
@property (nonatomic, strong) BHInfiniteScrollView *bannerView;

@property (nonatomic, strong) UIView *footerView;
@end

@implementation HomeTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.normalArr = [NSMutableArray array];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[CommodityCell class] forCellReuseIdentifier:@"commodityCell"];
//    self.tableView.tableFooterView = self.footerView;
    
    __weak typeof(self) weakSelf = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (weakSelf.headerRefresh) {
            weakSelf.headerRefresh();
        }
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (weakSelf.footerRefresh) {
            weakSelf.footerRefresh();
        }
    }];
}

- (void)setShowArr:(NSArray *)arr withPageNation:(PageNationModel *)pageNationModel {
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
    if (pageNationModel.totalPage == pageNationModel.pageNumber) {
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    } else {
        [self.tableView.mj_footer resetNoMoreData];
    }
    
    self.normalArr = [NSMutableArray arrayWithArray:arr];

    [self.tableView  reloadData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [CommodityCell getRowNumberWithItemArr:self.normalArr];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CommodityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"commodityCell" forIndexPath:indexPath];
    [cell setValueForCell:[CommodityCell getCellItemArr:self.normalArr withIndexPath:indexPath]];
    __weak typeof(self) weakSelf = self;
    cell.block = ^(NSInteger index) {
        CommodityModel *model = [weakSelf.normalArr safeObjectAtIndex:indexPath.row * 2 + index];
        if (weakSelf.block) {
            weakSelf.block(model.gid);
        }
    };
    return cell;

}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [CommodityCell getHeight];
}

- (UIView *)footerView {
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
        
        
        NSString *footStr = @"再怎么翻也没有了";
        NSDictionary *attributes = @{NSFontAttributeName:TextNormalFont12};
        CGFloat length = [footStr boundingRectWithSize:CGSizeMake(ScreenWidth, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        length = ceilf(length);
        
        // 文字
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, length + 30, 40)];
        titleLabel.text = footStr;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = TextNormalFont12;
        titleLabel.textColor = [UIColor grayTextColor];
        titleLabel.backgroundColor = [UIColor whiteColor];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, titleLabel.width + 120, 0.5)];
        lineView.backgroundColor = [UIColor grayTextColor];
        lineView.center = _footerView.center;
        titleLabel.center = lineView.center;


        [_footerView addSubview:lineView];
        [_footerView addSubview:titleLabel];
    }
    return _footerView;
}

#pragma mark - Getters
- (UIView *)tableHeaderView {
    if (!_tableHeaderView) {
        _tableHeaderView = [[UIView alloc] init];
        _tableHeaderView.backgroundColor = [UIColor grayDDColor];
    }
    return _tableHeaderView;
}

@end
